package com.zsck.pos.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chinaums.commoncheck.R;
import com.zsck.pos.common.ClickListenerProxy;

@SuppressLint({"NewApi", "ValidFragment"})
public class DialogManager extends BaseDialogFragment {
    private float def = 0.7f;
    protected View mRootView;
    private TextView tv_title;
    private TextView tv_confirm;
    private String title = "";
    private String confirm = "";
    private String tips = "";
    private String cancel = "";
    private TextView tv_tips;
    private TextView tv_cancel;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.dailog_manager, container, false);
        initView(mRootView);
        initData();
        return mRootView;
    }

    public DialogManager(String title, String confirm, String tips, String cancel) {
        this.title = title;
        this.confirm = confirm;
        this.tips = tips;
        this.cancel = cancel;
    }

    public DialogManager(Context context, int title, int confirm, int tips, int cancel) {
        this.title = context.getString(title);
        this.confirm = context.getString(confirm);
        this.tips = context.getString(tips);
        this.cancel = context.getString(cancel);
    }
    public DialogManager(Context context, String title, String tips) {
        this.title = title;
        this.confirm = context.getString(R.string.confirm);
        this.tips = tips;
        this.cancel = context.getString(R.string.cancel);
    }
    public DialogManager(Context context, int title, int tips) {
        this.title = context.getString(title);
        this.confirm = context.getString(R.string.confirm);
        this.tips = context.getString(tips);
        this.cancel = context.getString(R.string.cancel);
    }


    private void initView(View rootView) {
        tv_title = rootView.findViewById(R.id.tv_title);
        tv_confirm = rootView.findViewById(R.id.tv_confirm);
        tv_cancel = rootView.findViewById(R.id.tv_cancel);
        tv_tips = rootView.findViewById(R.id.tv_tips);
        tv_confirm.setOnClickListener(ClickListenerProxy.getProxy(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onSureClick != null) {
                    onSureClick.onSureClick();
                }
            }
        }));

        tv_cancel.setOnClickListener(ClickListenerProxy.getProxy(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        }));

    }

    public interface onSureClick {
        void onSureClick();
    }

    private onSureClick onSureClick;

    public void setOnSureClick(onSureClick onSureClick) {
        this.onSureClick = onSureClick;
    }

    private void initData() {

        if (!TextUtils.isEmpty(title)) {
            tv_title.setText(title);
        }
        if (!TextUtils.isEmpty(confirm)) {
            tv_confirm.setText(confirm);
        }
        if (!TextUtils.isEmpty(cancel)) {
            tv_cancel.setText(cancel);
        }

        if (TextUtils.isEmpty(tips)) {
            tv_tips.setVisibility(View.GONE);
        } else {
            tv_tips.setVisibility(View.VISIBLE);
            tv_tips.setText(tips);
        }
    }


    private onDialogFragmentDisMiss mOnDialogFragmentDisMiss;

    public interface onDialogFragmentDisMiss {
        void onDialogFragmentDisMiss();
    }

    public void setOnDialogFragmentDisMiss(onDialogFragmentDisMiss onDialogFragmentDisMiss) {
        this.mOnDialogFragmentDisMiss = onDialogFragmentDisMiss;
    }

    @Override
    protected void onSetPosition() {
        super.onSetPosition();
        setPosition(def, WRAP_CONTENT, DEFAULT_GRAVITY);
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        if (mOnDialogFragmentDisMiss != null) {
            mOnDialogFragmentDisMiss.onDialogFragmentDisMiss();
        }
    }
}
